<?php
// 档案管理
namespace Home\Controller;

use Think\Controller;

class ArchivesController extends Controller
{

    public function _initialize()
    {
        if (empty($_SESSION['empid'])) {

            $this->error('页面加载中。。。', U('Login/Login'), 1);
        }
    }

    public function Teacher()
    {
        $teacher = D('CTeacher');
        $param=I('param.');
        unset($param['p']);
        if ($param) {
            $tid = $param['tid'];
            $tname = $param['tname'];
            $sex = $param['sex'];
            $start = $param['start'];
            $end = $param['end'];
//            $tid = I('post.tid');
//            $tname = I('post.tname');
//            $sex = I('post.sex');
//            $start = I('post.start');
//            $end = I('post.end');
            session('tid', null);
            session('tname', null);
            session('start', null);
            session('end', null);
            if (!empty($tid)) {
                session('tid', $tid);
                $map['tid'] = array(
                    'eq',
                    $tid
                );
            }
            if (!empty($tname)) {
                $map['tname'] = array(
                    'like',
                    '%' . $tname . '%'
                );
                session('tname', $tname);
            }
            if (!empty($sex)) {
                $map['sex'] = array(
                    'eq',
                    $sex
                );
            }
            if (!empty($start) && !empty($end)) {
                session('start', $start);
                session('end', $end);
                $map['EntryDate'] = array(
                    'between',
                    '' . $start . ',' . $end . ''
                );
            } elseif (!empty($start)) {
                $map['EntryDate'] = array(
                    'EGT',
                    $start
                );
            } elseif (!empty($end)) {
                $map['EntryDate'] = array(
                    'ELT',
                    $end
                );
            }

            $count = $teacher->where($map)->count(); // 查询满足要求的总记录数

            $Page = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->setConfig('header', '共%TOTAL_ROW%条');
            $Page->setConfig('first', '首页');
            $Page->setConfig('last', '共%TOTAL_PAGE%页');
            $Page->setConfig('prev', '上一页');
            $Page->setConfig('next', '下一页');
            $Page->setConfig('link', 'indexpagenumb'); // pagenumb 会替换成页码
            $Page->setConfig('theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');

            $show = $Page->show(); // 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $tinfo = $teacher->where($map)
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->select();
//var_dump($map);
            $ssql = $teacher->where($map)
                ->fetchSql(true)
                ->select();
            session('ssql', $ssql);
        } else {
            $count = $teacher->count(); // 查询满足要求的总记录数

            $Page = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->setConfig('header', '共%TOTAL_ROW%条');
            $Page->setConfig('first', '首页');
            $Page->setConfig('last', '共%TOTAL_PAGE%页');
            $Page->setConfig('prev', '上一页');
            $Page->setConfig('next', '下一页');
            $Page->setConfig('link', 'indexpagenumb'); // pagenumb 会替换成页码
            $Page->setConfig('theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');

            $show = $Page->show(); // 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $tinfo = $teacher->limit($Page->firstRow . ',' . $Page->listRows)->select();
            $ssql = $teacher->fetchSql(true)->select();
            session('ssql', $ssql);
        }

        $this->assign('page', $show); // 赋值分页输出
        // echo $ssql;
        $this->assign('tinfo', $tinfo);
        $this->display(); // 输出模板
    }

    public function Daochu()
    {
        $sql = session('ssql');
        $fs = I('get.fs');
        R('Excel/Out', array(
            $sql,
            $fs
        ));
    }

    public function Addteacher()
    {
        if (IS_AJAX) {

            $teacher = M('CTeacher');
            $cf['tid'] = I('post.tid');
            $tid = $teacher->where($cf)->find();
            if ($tid) {

                $data = 3;
            } else {
                $teacher->create();
                $add = $teacher->add();
                if ($add) {

                    $data = 0;
                } else {
                    $data = 1;
                }
            }

            echo $data;
        } else {
            $this->display();
        }
    }

    public function del()
    {
        $id = I('get.id');
        $map['id'] = $id;
        if (I('get.lb') == 'teacher') {
            $teacher = M('CTeacher');

            $del = $teacher->where($map)->delete();
            if ($del) {

                $this->success('删除成功');
            } else {

                $this->error('删除失败');
            }
        } elseif (I('get.lb') == 'student') {
            $student = M('CStudent');

            $del = $student->where($map)->delete();
            if ($del) {

                $this->success('删除成功');
            } else {

                $this->error('删除失败');
            }
        }
    }

    public function Teacherxg()
    {
        $teacher = M('CTeacher');

        $this->assign('usinfo', $userinfo);
        if (IS_AJAX) {
            $teacher->create();
            $updata = $teacher->save(); // 根据条件保存修改的数据
            if ($updata) {

                $data = 0;
            } else {

                $data = 1;
            }
            echo $data;
        } else {

            $map['id'] = I('get.id');
            $us = $teacher->where($map)->find();
            $this->assign('us', $us);
            $this->display();
        }
    }

    public function Student()
    {
        $student = D('CStudent');
        $param=I('param.');
        unset($param['p']);
        if ($param) {
            $userid = $param['userid'];
            $classid = $param['classid'];
            $level = $param['level'];
            $company = $param['company'];
            $political = $param['political'];
            $idcard = $param['idcard'];
            $loginnames = $param['loginname'];
            $room = I('post.room');
            $room = I('post.car');


            if (!empty($loginnames)) {

                $map['loginname'] = array(
                    'like',
                    '%' . $loginnames . '%'
                );
            }
            if (!empty($userid)) {


                $map['userid'] = array(
                    'eq',
                    $userid
                );
            }
            if (!empty($classid)) {
                $map['classid'] = array(
                    'eq',
                    $classid
                );
            }
            if (!empty($level)) {
                $map['level'] = array(
                    'eq',
                    $level
                );
            }
            if (!empty($company)) {

                $map['Company'] = array(
                    'eq',
                    $company
                );
            }
            if (!empty($political)) {

                $map['Political'] = array(
                    'eq',
                    $political
                );
            }
            if (!empty($idcard)) {
                $map['IDcard'] = array(
                    'eq',
                    $idcard
                );
            }

            $count = $student->where($map)->count(); // 查询满足要求的总记录数
            $Page = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->setConfig('header', '共%TOTAL_ROW%条');
            $Page->setConfig('first', '首页');
            $Page->setConfig('last', '末页');
            $Page->setConfig('prev', '上一页');
            $Page->setConfig('next', '下一页');
            $Page->lastSuffix = false;
            $Page->setConfig('link', 'indexpagenumb'); // pagenumb 会替换成页码
            $Page->setConfig('theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');

            $show = $Page->show(); // 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $tinfo = $student->where($map)
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->order('classid desc')
                ->Relation(true)
                ->select();

            $ssql = $student->where($map)
                ->order('classid desc')
                ->fetchSql(true)
                ->select();
            session('ssql', $ssql);
        } else {
            $count = $student->count(); // 查询满足要求的总记录数

            $Page = new \Think\Page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->setConfig('header', '共%TOTAL_ROW%条');
            $Page->setConfig('first', '首页');
            $Page->setConfig('end', '末页');
            $Page->setConfig('prev', '上一页');
            $Page->setConfig('next', '下一页');
            $Page->setConfig('link', 'indexpagenumb'); // pagenumb 会替换成页码
            $Page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE%%DOWN_PAGE%%END%<%HEADER% ');

            $show = $Page->show(); // 分页显示输出
            // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
            $tinfo = $student->limit($Page->firstRow . ',' . $Page->listRows)
                ->order('classid desc')
                ->Relation(true)
                ->select();
            // dump($tinfo);
            $ssql = $student->order('classid desc')
                ->fetchSql(true)
                ->select();
            session('ssql', $ssql);
        }

        $this->assign('page', $show); // 赋值分页输出
        $stulb = R('Pub/Stulb');
        $room = R('Pub/Room');
        $this->assign('room', $room);
        $room = R('Pub/Car');
        $this->assign('car', $car);
        $this->assign('param',$param);
        $this->assign('stulb', $stulb);
        $this->assign('tinfo', $tinfo);
        $this->display(); // 输出模板
    }

    public function Studentxg()
    {
        $student = M('CStudent');

        if (IS_AJAX) {
            $student->create();
            $updata = $student->save(); // 根据条件保存修改的数据
            if ($updata) {

                $data = 0;
            } else {

                $data = 1;
            }
            echo $data;
        } else {
            $classs = R('Pub/Sche');
            $stulb = R('Pub/Stulb');
            $this->assign('stulb', $stulb);
            // dump($stulb);
            $this->assign('classs', $classs);
            $map['id'] = I('get.id');
            $us = $student->where($map)->find();
            $this->assign('us', $us);
            $room = R('Pub/Room');

            $this->assign('room', $room);
            $car = R('Pub/Car');
            $this->assign('car', $car);
            var_dump($s);
            $this->display();
        }
    }

    public function Addstudent()
    {
        if (IS_AJAX) {

            $student = M('CStudent');
            $cf['userid'] = I('post.userid');
            $tid = $student->where($cf)->find();
            if ($tid) {

                $data = 3;
            } else {
                $student->create();
                $add = $student->add();
                if ($add) {

                    $data = 0;
                } else {
                    $data = 1;
                }
            }

            echo $data;
        } else {
            $classs = R('Pub/Sche');
            $stulb = R('Pub/Stulb');
            $this->assign('stulb', $stulb);
            // dump($stulb);
            $this->assign('classs', $classs);

            $this->display();
        }
    }
    // put your code here
}
